﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;
using Utils;

public partial class UserControls_blockcard : BaseUserControl
{
    // Fields
    protected NameValueCollection order = new NameValueCollection();
    protected NameValueCollection system = new NameValueCollection();
    protected NameValueCollection team = new NameValueCollection();

    // Methods
    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        this.system = Helper.GetObjectProtery(base.ASSystem);
        if (((base.Request["submit"] == "确认使用") && (base.Request.Form["order_id"] != null)) && (base.Request["cardcode"] != null))
        {
            int @int = Helper.GetInt(base.Request.Form["order_id"], 0);
            decimal @decimal = Helper.GetDecimal(this.system["card"], 0M);
            string id = Helper.GetString(base.Request.Form["cardcode"], string.Empty);
            Maticsoft.BLL.Order order = new Maticsoft.BLL.Order();
            Maticsoft.Model.Order model = new Maticsoft.Model.Order();
            Maticsoft.BLL.Card card = new Maticsoft.BLL.Card();
            Maticsoft.Model.Card card2 = new Maticsoft.Model.Card();
            card2 = card.GetModel(base.Request["cardcode"].ToString());
            model = order.GetModel(@int);
            if (model != null)
            {
                if (model.Card > 0M)
                {
                    base.SetError("您已经使用过代金券了");
                    base.Response.Redirect(base.Request.Url.AbsoluteUri);
                    base.Response.End();
                }
                else
                {
                    Maticsoft.Model.Team team = new Maticsoft.Model.Team();
                    team = new Maticsoft.BLL.Team().GetModel(model.Team_id);
                    Maticsoft.Model.Card card3 = new Maticsoft.Model.Card();
                    card3 = new Maticsoft.BLL.Card().GetModel(id);
                    if (card3 == null)
                    {
                        base.SetError("您输入的代金券不存在");
                        base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        base.Response.End();
                    }
                    else if ((card3.Partner_id != team.Partner_id) && (card3.Partner_id > 0))
                    {
                        base.SetError("您输入的代金券不存在");
                        base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        base.Response.End();
                    }
                    else if ((card3.Begin_time > DateTime.Now) || (card3.End_time < DateTime.Now))
                    {
                        base.SetError("您输入的代金券已过期");
                        base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        base.Response.End();
                    }
                    else if (card3.consume != "Y")
                    {
                        decimal num3 = Math.Min(card3.Credit, team.Card);
                        model.Card = num3;
                        if (model.Card >= model.Origin)
                        {
                            model.Origin = 0M;
                        }
                        else
                        {
                            model.Origin -= model.Card;
                        }
                        model.Card_id = base.Request["cardcode"].ToString();
                        order.Update(model);
                        card2.Team_id = team.Id;
                        card2.Partner_id = team.Partner_id;
                        card2.Order_id = model.Id;
                        card2.consume = "Y";
                        card.Update(card2);
                        base.SetSuccess("代金券使用成功");
                        base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        base.Response.End();
                    }
                    else
                    {
                        base.SetError("您输入的代金券已被使用，请输入其他代金券");
                        base.Response.Redirect(base.Request.Url.AbsoluteUri);
                        base.Response.End();
                    }
                }
            }
        }
    }

    public void UpdateView(int orderid)
    {
        Maticsoft.Model.Order model = new Maticsoft.BLL.Order().GetModel(orderid);
        int id = 0;
        if (model != null)
        {
            id = model.Team_id;
        }
        this.order = Helper.GetObjectProtery(model);
        Maticsoft.Model.Team team = new Maticsoft.Model.Team();
        team = new Maticsoft.BLL.Team().GetModel(id);
        if (team != null)
        {
            if ((team.Card <= 0) || (model.Card > 0M))
            {
                this.Visible = false;
            }
            else
            {
                this.Visible = true;
            }
        }
        else
        {
            this.Visible = false;
        }
        this.team = Helper.GetObjectProtery(team);
    }

}